export default {
  data: function () {
    return {
      showEntityDialog: false,
      entityQueryForm: {},
      operationType: "",
      entityList: [],
      entity: {},
      total: 0,
      currentPage: 1,
      pageSize: 10,
      sortInfo: null
    }
  },
  methods: {
    addEntity() {
      let url = `/${this.entityName4LowerCamel}/${this.entityName4LowerCamel}/`;
      this.ajax(url, this.entity, "POST").then(res => {
        if (res.success) {
          this.getEntityListByPage();
        }
      })
    },
    async deleteEntity(index, row) {
      this.$confirm('确定要删除?', '提示', {confirmButtonText: '确定', cancelButtonText: '取消', type: 'warning', center:true }).then(() => {
        let url = `/${this.entityName4LowerCamel}/${this.entityName4LowerCamel}/` + row[this.primaryKey];
        this.ajax(url,null,'delete').then((res) => {
          if (res.success) {
            this.getEntityListByPage();
          }
        })
      }).catch(() => {
        this.$message({type: 'info', message: '已取消删除'});
      });
    },
    async updateEntity() {
      let url = `/${this.entityName4LowerCamel}/${this.entityName4LowerCamel}/`;
      let res = await this.ajax(url, this.entity, "put");
      if (res.success) {
        this.getEntityListByPage();
      }
    },
    async getEntityListByPage() {
      let url = `/${this.entityName4LowerCamel}/get${this.entityName4UpperCamel}ListByPage`;
      // 组装分页与排序
      this.entityQueryForm.currentPage = this.currentPage;
      this.entityQueryForm.pageSize = this.pageSize;
      this.entityQueryForm.sortInfo = this.sortInfo;
      let res = await this.ajax(url, this.entityQueryForm, 'POST');
      if (res.success) {
        this.entityList = res.data.list;
        this.total = res.data.total;
      }
    },
    showUpdateEntityDialogHandler(index, row) {
      this.operationType = "update";
      this.entity = row;
      this.showEntityDialog = true;
    },
    showAddEntityDialogHandler() {
      this.operationType = "add";
      this.entity = {};
      this.showEntityDialog = true;
    },
    entityDialogConfirm() {
      if (this.operationType === "add") {
        this.addEntity()
      }
      if (this.operationType === "update") {
        this.updateEntity()
      }
      this.showEntityDialog = false;
    },
    sizeChangeHandle(pageSize) {
      this.pageSize = pageSize;
      this.getEntityListByPage();
    },
    currentChangeHandle(currentPage) {
      this.currentPage = currentPage;
      this.getEntityListByPage();
    },
  },
  mounted() {
    this.getEntityListByPage()
  }
}
